package com.pimee.web.sys;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.pimee.common.config.UbConfig;
import com.pimee.common.exception.BusinessException;
import com.pimee.common.util.DateUtil;
import com.pimee.common.util.StringUtils;
import com.pimee.common.util.file.FileUploadUtils;
import com.pimee.model.SysUser;
import com.pimee.model.vo.SysUserVo;
import com.pimee.service.admin.ISysUserService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

/**
 * 
 * @author Bruce Shaw 2020年2月4日 下午10:53:39
 *
 */
@Controller
@RequestMapping("/admin/sys/user/profile")
public class SysProfileController extends AbstractController {

	private String prefix = "admin/sys/user/profile";

	@Autowired
	private ISysUserService sysUserService;

	@ApiOperation("获取个人信息")
	@GetMapping()
	public String profile(ModelMap mmap) {
		Long userId = super.getUserId();
		SysUserVo sysUserVo = sysUserService.selectUserById(userId);
		mmap.put("user", sysUserVo);
		mmap.put("roleGroup", sysUserService.selectUserRoleGroup(userId));
		mmap.put("postGroup", sysUserService.selectUserPostGroup(userId));
		return prefix + "/profile";
	}

	@ApiOperation("校验密码是否匹配")
	@GetMapping("/checkPassword")
	@ResponseBody
	public boolean checkPassword(String password) {
		SysUser user = super.getSysUser();
		return sysUserService.checkPassword(user, password);
	}

	@ApiOperation("进入重置密码")
	@GetMapping("/resetPwd")
	public String resetPwd(ModelMap modelMap) {
		SysUser user = super.getSysUser();
		modelMap.put("user", sysUserService.selectUserById(user.getUserId()));
		return prefix + "/resetPwd";
	}

	@ApiOperation("重置密码")
	@PostMapping("/resetPwd")
	@ResponseBody
	public Object resetPwd(String oldPassword, String newPassword, ModelMap modelMap) {
		SysUser user = super.getSysUser();
		if (StringUtils.isNotEmpty(newPassword) && sysUserService.checkPassword(user, oldPassword)) {
			user.setPassword(newPassword);
			sysUserService.resetUserPwd(user);
			user = sysUserService.selectUserById(user.getUserId());
			return this.setSuccessModelMap(modelMap, user);
		} else {
			throw new BusinessException("修改密码失败，旧密码错误");
		}
	}

	@ApiOperation("修改用户")
	@GetMapping("/edit")
	public String edit(ModelMap modelMap) {
		SysUser user = super.getSysUser();
		modelMap.put("user", sysUserService.selectUserById(user.getUserId()));
		return prefix + "/edit";
	}

	@ApiOperation("进入修改头像")
	@GetMapping("/avatar")
	public String avatar(ModelMap modelMap) {
		SysUser user = super.getSysUser();
		modelMap.put("user", sysUserService.selectUserById(user.getUserId()));
		return prefix + "/avatar";
	}

	@ApiOperation("修改用户")
	@PostMapping("/update")
	@ResponseBody
	public Object update(SysUser user, ModelMap modelMap) {
		SysUser currentUser = super.getSysUser();
		currentUser.setUserName(user.getUserName());
		currentUser.setEmail(user.getEmail());
		currentUser.setPhonenumber(user.getPhonenumber());
		currentUser.setSex(user.getSex());
		currentUser.setUpdateBy(super.getLoginName());
		currentUser.setUpdateTime(DateUtil.now());
		sysUserService.updateNotNull(currentUser);
		SysUserVo sysUserVo = sysUserService.selectUserById(currentUser.getUserId());
		return this.setSuccessModelMap(modelMap, sysUserVo);
	}

	@ApiOperation("保存用户修改头像")
	@PostMapping("/updateAvatar")
	@ResponseBody
	public Object updateAvatar(@RequestParam("avatarfile") MultipartFile file, ModelMap modelMap) {
		SysUser currentUser = super.getSysUser();
		if (!file.isEmpty()) {
			String avatar;
			try {
				avatar = FileUploadUtils.upload(UbConfig.getAvatarPath(), file);
				currentUser.setAvatar(avatar);
				sysUserService.updateNotNull(currentUser);
				SysUser user = sysUserService.selectUserById(currentUser.getUserId());
				return this.setSuccessModelMap(modelMap, user);
			} catch (IOException e) {
				throw new BusinessException("保存头像失败，原因：上传失败");
			}
		} else {
			throw new BusinessException("保存头像失败，原因：文件为空");
		}
	}
}
